<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--      <el-form-item label="客户姓名" prop="workCreatorName">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workCreatorName"-->
      <!--          placeholder="请输入客户姓名"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="设备Mac" prop="deviceId">
        <el-input
          v-model="queryParams.deviceId"
          placeholder="请输入设备Mac"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="故障类型" prop="faultType">
        <el-select v-model="queryParams.faultType" placeholder="请选择故障类型" clearable>
          <el-option
            v-for="dict in dict.type.fault_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="故障发生时间" prop="occurredAt">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.occurredAt"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择故障发生时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="故障解决时间" prop="resolvedAt">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.resolvedAt"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择故障解决时间">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="故障持续时间" prop="duration">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.duration"-->
      <!--          placeholder="请输入故障持续时间"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="故障严重程度" prop="severity">
        <el-select v-model="queryParams.severity" placeholder="请选择故障严重程度" clearable>
          <el-option
            v-for="dict in dict.type.fault_severity"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <!--      <el-form-item label="设备位置" prop="location">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.location"-->
      <!--          placeholder="请输入设备位置"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="制造商" prop="manufacturer">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.manufacturer"-->
      <!--          placeholder="请输入制造商"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="维修进度" prop="schedule">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.schedule"-->
      <!--          placeholder="请输入维修进度"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="维修日期" prop="repairDate">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.repairDate"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择维修日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="完成日期" prop="completionDate">-->
      <!--        <el-date-picker clearable-->
      <!--          v-model="queryParams.completionDate"-->
      <!--          type="date"-->
      <!--          value-format="yyyy-MM-dd"-->
      <!--          placeholder="请选择完成日期">-->
      <!--        </el-date-picker>-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="工单ID" prop="workOrderId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.workOrderId"-->
      <!--          placeholder="请输入工单ID"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <!--      <el-form-item label="维修人员人ID" prop="creatorId">-->
      <!--        <el-input-->
      <!--          v-model="queryParams.creatorId"-->
      <!--          placeholder="请输入维修人员人ID"-->
      <!--          clearable-->
      <!--          @keyup.enter.native="handleQuery"-->
      <!--        />-->
      <!--      </el-form-item>-->
      <el-form-item label="维修人员姓名" prop="creatorName">
        <el-input
          v-model="queryParams.creatorName"
          placeholder="请输入维修人员姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

<!--    <el-row :gutter="10" class="mb8">-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['system:orders:add']"-->
<!--        >新增-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['system:orders:edit']"-->
<!--        >进度上传-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      &lt;!&ndash;      <el-col :span="1.5">&ndash;&gt;-->
<!--      &lt;!&ndash;        <el-button&ndash;&gt;-->
<!--      &lt;!&ndash;          type="danger"&ndash;&gt;-->
<!--      &lt;!&ndash;          plain&ndash;&gt;-->
<!--      &lt;!&ndash;          icon="el-icon-delete"&ndash;&gt;-->
<!--      &lt;!&ndash;          size="mini"&ndash;&gt;-->
<!--      &lt;!&ndash;          :disabled="multiple"&ndash;&gt;-->
<!--      &lt;!&ndash;          @click="handleDelete"&ndash;&gt;-->
<!--      &lt;!&ndash;          v-hasPermi="['system:orders:remove']"&ndash;&gt;-->
<!--      &lt;!&ndash;        >删除&ndash;&gt;-->
<!--      &lt;!&ndash;        </el-button>&ndash;&gt;-->
<!--      &lt;!&ndash;      </el-col>&ndash;&gt;-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['system:orders:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->
<!--      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
<!--    </el-row>-->

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--      <el-table-column label="订单ID" align="center" prop="id" />-->
      <!--      <el-table-column label="客户姓名" align="center" prop="workCreatorName" />-->
      <el-table-column label="设备Mac" align="center" prop="deviceId"/>
      <el-table-column label="故障类型" align="center" prop="faultType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fault_type" :value="scope.row.faultType"/>
        </template>
      </el-table-column>
      <el-table-column label="故障描述" align="center" prop="description"/>
      <el-table-column label="故障发生时间" align="center" prop="occurredAt" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.occurredAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="故障解决时间" align="center" prop="resolvedAt" width="180">-->
      <!--        <template slot-scope="scope">-->
      <!--          <span>{{ parseTime(scope.row.resolvedAt, '{y}-{m}-{d}') }}</span>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
      <el-table-column label="故障持续时间" align="center" prop="duration"/>
      <el-table-column label="故障严重程度" align="center" prop="severity">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.fault_severity" :value="scope.row.severity"/>
        </template>
      </el-table-column>
      <el-table-column label="其他备注信息" align="center" prop="notes"/>
      <el-table-column label="设备位置" align="center" prop="location"/>
      <el-table-column label="制造商" align="center" prop="manufacturer"/>
      <el-table-column label="维修进度" align="center" prop="schedule"/>
      <el-table-column label="维修日期" align="center" prop="repairDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成日期" align="center" prop="completionDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <!--      <el-table-column label="工单ID" align="center" prop="workOrderId" />-->
      <!--      <el-table-column label="维修人员人ID" align="center" prop="creatorId" />-->
      <el-table-column label="维修人员姓名" align="center" prop="creatorName"/>
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['system:orders:edit']"-->
      <!--          >修改-->
      <!--          </el-button>-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-delete"-->
      <!--            @click="handleDelete(scope.row)"-->
      <!--            v-hasPermi="['system:orders:remove']"-->
      <!--          >删除-->
      <!--          </el-button>-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修接单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单信息" prop="faultId">
          <el-select v-model="form.faultId" placeholder="请选择工单信息">
            <el-option
              v-for="dict in workOrderlist"
              :key="dict.id"
              :label="dict.faultMac"
              :value="dict.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVisible" @open="onOpen" @close="onClose" title="维修进度">
      <el-form ref="form" :model="form" size="medium" label-width="100px">
        <el-form-item label="维修进度" prop="schedule" required>
          <el-slider :min="0" :max='100' :step='1' v-model="form.schedule" :tooltip-format="formatTooltip"></el-slider>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="handleConfirm">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listOrders, getOrders, delOrders, addOrders, updateOrders, listOrdersInfo} from "@/api/system/orders";
import {listWorkorders} from "@/api/system/workorders";

export default {
  methods: {
    /** 查询维修接单列表 */
    getList() {
      this.loading = true;
      listOrdersInfo(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        workCreatorName: null,
        deviceId: null,
        faultType: null,
        description: null,
        occurredAt: null,
        resolvedAt: null,
        duration: null,
        severity: null,
        notes: null,
        location: null,
        manufacturer: null,
        schedule: null,
        repairDate: null,
        completionDate: null,
        workOrderId: null,
        creatorId: null,
        creatorName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      listWorkorders({
        pageNum: 1,
        pageSize: 10000,
        status: 2
      }).then(res => {
        console.log("获取故障信息.....", res)
        this.workOrderlist = res.rows
      })
      this.open = true;
      this.open = true;
      this.title = "添加维修接单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {

      this.reset();
      const id = row.id || this.ids
      getOrders(id).then(response => {
        this.form = response.data;
        this.title = "修改维修接单";
        console.log("aaaa")
        console.log(this.form.schedule)
        if (this.form.schedule === 100) {
          this.$modal.alertError("维修进度已经达到100%不可再次上传")
        }else {
          this.dialogVisible = true;
        }
      });


    },
    formatTooltip(val) {
      return `当前大小: ${val}`;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.workOrderId = this.workOrderlist[0].id
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除维修接单编号为"' + ids + '"的数据项？').then(function () {
        return delOrders(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    },
    onOpen() {
      this.dialogVisible = true;
    },
    onClose() {
      this.dialogVisible = false
    },
    close() {
      this.dialogVisible = false
    },
    handleConfirm() {
      updateOrders(this.form).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.dialogVisible = false;
        this.getList();
      });
    },
  },
  name: "Orders",
  dicts: ['fault_type', 'fault_severity'],
  data() {
    return {
      // 遮罩层
      loading: true,
      dialogVisible: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修接单表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      formData: {
        field101: 66,
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        workCreatorName: null,
        deviceId: null,
        faultType: null,
        description: null,
        occurredAt: null,
        resolvedAt: null,
        duration: null,
        severity: null,
        notes: null,
        location: null,
        manufacturer: null,
        schedule: null,
        repairDate: null,
        completionDate: null,
        workOrderId: null,
        creatorId: null,
        creatorName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        workCreatorName: [
          {required: true, message: "客户姓名不能为空", trigger: "blur"}
        ],
      },
      workOrderlist: []
    };
  },
  created() {
    this.getList();
  }
};
</script>
